﻿#include "C3fdProject.h"
#include <QDir>
#include <QCoreApplication>

namespace Common
{

	C3fdProject *C3fdProject::m_Instance = new C3fdProject;

	C3fdProject *C3fdProject::instance()
	{
		return m_Instance;
	}

	QString C3fdProject::getCurrentProjectDir() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getWorkDir();
		}
		else
			return "";
	}
	void C3fdProject::setCurrentProjectDir(QString dir)
	{
		QString path = QDir::cleanPath(dir) + QDir::separator();
		if (m_CurrentCase != nullptr)
		{
			delete m_CurrentCase;
		}
		m_CurrentCase = new C3fdCase(path);

		if (QFile::exists(path + "Case.c3fd")) {
			m_CurrentCase->load();
			m_FromXml = true;
		}
		else {
			m_CurrentCase->initCase();
			m_FromXml = false;
		}
	}

	QString C3fdProject::getCacheDir() const
	{
		return "data";
	}

	QString C3fdProject::getWorkingConditionDirName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("work_condition_dir");
		}
		else
			return "";
	}
	QString C3fdProject::getMeshDirName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("mesh_dir");
		}
		else
			return "";
	}
	QString C3fdProject::getMeshControlFileName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("mesh_control_file");
		}
		else
			return "";
	}
	QString C3fdProject::getMeshReportFileName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("mesh_report_file");
		}
		else
			return "";
	}
	QString C3fdProject::getMesh3DFileName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("3d_mesh_file");
		}
		else
			return "";
	}
	QString C3fdProject::getMeshS2FileName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("s2_mesh_file");
		}
		else
			return "";
	}
	QString C3fdProject::getMeshB2bHubFileName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("b2b_mesh_hub_file");
		}
		else
			return "";
	}
	QString C3fdProject::getMeshB2bMidFileName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("b2b_mesh_mid_file");
		}
		else
			return "";
	}
	QString C3fdProject::getMeshB2bTipFileName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("b2b_mesh_tip_file");
		}
		else
			return "";
	}
	QString C3fdProject::getMeshExeFilePath() const
	{
		return QCoreApplication::applicationDirPath() + "/solver/MESH.exe";
	}

	QString C3fdProject::getSolverInputDirName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("solver_input_dir");
		}
		else
			return "";
	}

	QString C3fdProject::getSolverOutputDirName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("solver_output_dir");
		}
		else
			return "";
	}

	QString C3fdProject::getSolverExeFilePath() const
	{
		return QCoreApplication::applicationDirPath() + "/solver/SOLVER.exe";
	}

	QString C3fdProject::getPostExeFileDir() const
	{
		return QCoreApplication::applicationDirPath() + "/solver/";
	}

	QString C3fdProject::getMeshUsedForSolverFileName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("mesh_used_for_solver_file");
		}
		else
			return "";
	}

	QString C3fdProject::getGeomInfoOfBladeFileName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("geom_info_of_blade_file");
		}
		else
			return "";
	}

	QString C3fdProject::getSolverControlFileName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("solver_control_file");
		}
		else
			return "";
	}

	QString C3fdProject::getSolverTbmodelFileName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("solver_tbmodel_file");
		}
		else
			return "";
	}

	QString C3fdProject::getSolverBoundaryFileName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("solver_boundary_file");
		}
		else
			return "";
	}

	QString C3fdProject::getSolverPerfCurveFileName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("solver_perfcurve_file");
		}
		else
			return "";
	}

	QString C3fdProject::getSolverMonitorFileName() const
	{
		if (m_CurrentCase != nullptr)
		{
			return m_CurrentCase->getNameByKey("solver_monitor_file");
		}
		else
			return "";
	}

	C3fdCase *C3fdProject::getCurrentCase()
	{
		return m_CurrentCase;
	}

	bool C3fdProject::isFromXml()
	{
		return m_FromXml;
	}

	C3fdProject::~C3fdProject()
	{
		if (m_CurrentCase != nullptr)
		{
			delete m_CurrentCase;
			m_CurrentCase = nullptr;
		}
	}
}
